package com.xzy.dao.impl;

import com.xzy.dao.IUserDao;
import com.xzy.domain.User;
import com.xzy.utils.jdbcUtils;

import java.sql.Connection;

public class UserDaoImpl extends BaseDao<User> implements IUserDao {
    @Override
    public User queryUserByUsername(String username) {
        Connection conn = jdbcUtils.getConnection();
        String sql = "select id,username,password,email from t_user where username = ?";
        User user = queryForOne(conn, sql, username);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return user;
    }

    @Override
    public int saveUser(User user) {
       Connection conn = jdbcUtils.getConnection();
       String sql = "insert into t_user(username,password,email) value(?,?,?)";

       int updataNum= update(conn,sql,user.getUsername(),user.getPassword(),user.getEmail());
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
       return updataNum;
    }

    @Override
    public User queryByUserinfo(String username, String password) {
        Connection conn = jdbcUtils.getConnection();
        String sql = "select id,username,password,email from t_user where username=? and password=?";
        User user = queryForOne(conn, sql, username,password);
        jdbcUtils.clossAndCommit();//因为获取连接时设置了手动提交所以必须调用该方法(提交并关闭)否则就会连接超时(数据库操作后一直不提交)
        return user;
    }
}
